﻿var lastLinkElem = null;
var lastLinkElemBg = null;
var linkOperation = "";

function loadLastLinkElements() {
    var elem = document.getElementById(lastLinkElem);
    var elemChild = elem.getElementsByTagName("td");
    document.getElementById('txtLinkName').value = elemChild[0].innerHTML;
    document.getElementById('txtLinkUrl').value = elemChild[1].innerHTML;
    document.getElementById('txtLinkDescription').value = elemChild[1].title;
}

function clearLinkPopUpElements() {
    document.getElementById('txtLinkName').value = '';
    document.getElementById('txtLinkUrl').value = '';
    document.getElementById('txtLinkDescription').value = '';
}

function selectLinkTableElement(elemID) {
    if (lastLinkElem != null) {
         document.getElementById(lastLinkElem).style.backgroundColor = lastLinkElemBg;
    }
    var elem = document.getElementById(elemID);
    lastLinkElem = elemID;
    lastLinkElemBg = elem.style.backgroundColor;
    elem.style.backgroundColor = "#FEF0C9";
}

function showLinkOperation(operation) {
    if (operation == "Add") {
        document.getElementById('btnLinkOperation').value = "Add Link";
        document.getElementById('tdOperation').innerHTML = "Add Link";
        clearLinkPopUpElements();
    }
    else if (operation == "Edit") {
        if (lastLinkElem == null) {
            alert('No Link Selected');
        }
        loadLastLinkElements();
        document.getElementById('btnLinkOperation').value = "Save Changes";
        document.getElementById('tdOperation').innerHTML = "Edit Link";
    }
    document.getElementById('divLinkPopup').style.display = '';
    RelocateAddLinkPopup();
    document.getElementById('divLinks').style.opacity = 0.5;
    linkOperation = operation;


    toggleLinkButtons(false);
}

function toggleLinkButtons(enable) {
    document.getElementById('btnAddLink').disabled = !enable;
    document.getElementById('btnEditLink').disabled = !enable;
    document.getElementById('btnDelete').disabled = !enable;
}

function CancelLinkOperation() {
    document.getElementById('divLinkPopup').style.display = 'none';
    document.getElementById('divLinks').style.opacity = 1.0;
    toggleLinkButtons(true);
}

function RelocateAddLinkPopup() {
    var popup = document.getElementById("divLinkPopup")
    if (popup.style.display != 'none') {
        var width = getViewPortWidth();
        width -= 500;
        width -= popup.style.width.replace("px", "");
        popup.style.left = width / 2 + 'px';
        popup.style.top = "50px";
    }
}



function PerformLinkOperation() {

    linkName = document.getElementById('txtLinkName').value;
    linkUrl = document.getElementById('txtLinkUrl').value;
    linkDescription = document.getElementById('txtLinkDescription').value;

    errMsg = "";
    if (linkUrl == "") {
        errMsg += "Url is Required<br/>";
    }

    document.getElementById('lblErrorMessage').innerHTML = errMsg;

    if (errMsg != "") {
        return;
    }


    var url = "Personal/Links.aspx";

    if (linkOperation == "Add") {
        url = url + "?addlink=1";
    }
    else if (linkOperation == "Edit") {
        url = url + "?editlink=1";
        try {
            linkNo = document.getElementById(lastLinkElem).getAttribute("linkNo");
        } catch (ex) {
            alert('No Link Selected');
            return;
        }
        url = url + "&linkNo=" + URLEncode(linkNo);
    }


    url = url + "&linkName=" + URLEncode(linkName);
    url = url + "&linkUrl=" + URLEncode(linkUrl);
    url = url + "&linkDescription=" + URLEncode(linkDescription);
    url = url + "&sid=" + Math.random();

    //alert(url);
    response = SubmitLinkForm(url);
    //alert(response);
    if (response.indexOf("Success") > -1) {
        loadXMLDoc('Personal/Links.aspx');
        if (lastLinkElem != null) {
            document.getElementById(lastLinkElem).style.backgroundColor = "#FEF0C9";
        }
        if (linkOperation == "Add") {
            alert("New Link Successfully Added");
        }
        else if (linkOperation == "Edit") {
            alert("Link Successfully Updated");
        }
    } else {
        document.getElementById('lblErrorMessage').innerHTML = response;
    }

    return false;
}


function deleteSelectedLink() {
    if (lastLinkElem == null) {
        alert('No Link Selected');
    }

    var proceed = confirm("Are you sure you want to delete this link?");
    if (proceed == false) {
        return;
    }
    

    var linkNo = document.getElementById(lastLinkElem).getAttribute("linkNo");
    var url = "Personal/Links.aspx";
    url = url + "?deletelink=1";
    url = url + "&linkNo=" + URLEncode(linkNo);

    //alert(url);
    response = SubmitLinkForm(url);
    //alert(response);
    if (response.indexOf("Success") > -1) {
        lastLinkElem = null;
        loadXMLDoc('Personal/Links.aspx');
        alert("Link Successfully Deleted");
    } else {
        document.getElementById('lblErrorMessage').innerHTML = response;
    }
}


function SubmitLinkForm(url) {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
    response = xmlhttp.responseText;
    response = response.substring(response.indexOf("<response>"), response.indexOf("</response"));
    return response;
}